using System;
using System.Data;
using System.Web;
using System.Collections;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.ComponentModel;
using System.IO;

namespace FileServer
{
    /// <summary>
    /// Summary description for Explorer
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [ToolboxItem(false)]
    public class Explorer : System.Web.Services.WebService
    {

        [WebMethod(EnableSession = true)]
        public bool Login(string userName, string password)
        {
            string[] users = File.ReadAllLines(AppDomain.CurrentDomain.BaseDirectory + "rfsdb.ini");
            for (int i = 0; i < users.Length; i++)
            {
                string[] xx = users[i].Split(' ');
                if (xx[0] == userName && xx[1] == password)
                {
                    Session["Token"] = userName;
                    return true;
                }
            }
            return false;
        }
        private void LoginValidCheck()
        {
            if (Session["Token"] == null)
                throw new Exception("Invalid login token");
        }
        [WebMethod(EnableSession = true)]
        public string GetRootPath()
        {
            LoginValidCheck();
            string root = System.Configuration.ConfigurationManager.AppSettings["RootPath"];
            if (root == null || root == "")
            {
                root = Directory.GetParent(AppDomain.CurrentDomain.BaseDirectory).Parent.FullName;
            }
            return root;
        }
        [WebMethod(EnableSession = true)]
        public string[] GetChildDirs(string dir)
        {
            LoginValidCheck();
            return Directory.GetDirectories(dir);
        }
        [WebMethod(EnableSession = true)]
        public string[] GetChildFiles(string dir)
        {
            LoginValidCheck();
             return Directory.GetFiles(dir);
        }
       
        [WebMethod(EnableSession = true)]
        public string DeleteFile(string file)
        {
            LoginValidCheck();
            try
            {
                File.Delete(file);
                return "ok";
            }
            catch (Exception ex)
            {
                return ex.Message;
            }
        }
        [WebMethod(EnableSession = true)]
        public string BackupAndDelete(string file)
        {
            LoginValidCheck();
            try
            {
                string newFile=AppDomain.CurrentDomain.BaseDirectory+"backup\\("+DateTime.Now.ToString("yyyyMMddHHmmss")+")"+Path.GetFileName(file);
                File.Move(file, newFile);
                return "ok";
            }
            catch (Exception ex)
            {
                return ex.Message;
            }
        }
        [WebMethod(EnableSession = true)]
        public string TestPermission(string dir)
        {
            LoginValidCheck();
            try
            {
                string path=dir+"\\test.txt";
                File.WriteAllText(path, "test");
                File.Delete(path);
                return "ok";
            }
            catch (Exception ex)
            {
                return ex.Message;
            }
        }

        [WebMethod(EnableSession = true)]
        public string RemoveFolder(string dir)
        {
            LoginValidCheck();
            try
            {
                Directory.Delete(dir, true);
                return "ok";
            }
            catch (Exception ex)
            {
                return ex.Message;
            }
        }
        [WebMethod(EnableSession = true)]
        public string CreateFolder(string dir)
        {
            LoginValidCheck();
            try
            {
                Directory.CreateDirectory(dir);
                return "ok";
            }
            catch (Exception ex)
            {
                return ex.Message;
            }
        }
        //
        [WebMethod(EnableSession = true)]
        public byte[] GetFile(string fileName)
        {
            LoginValidCheck();
            FileInfo fi = new FileInfo(fileName);
            if (fi.Length > 1024 * 1024 * 2)//2 MB
                return new byte[0];
            else
                return File.ReadAllBytes(fileName);
        }
        //
        [WebMethod(EnableSession = true)]
        public string Receive(string file, long begin, long len, byte[] data)
        {
            LoginValidCheck();
            string _fileName = file;
            try
            {
                Stream _fs = File.OpenWrite(_fileName);
                _fs.Position = begin;
                _fs.Write(data, 0, (int)len);
                _fs.Flush();
                _fs.Close();
                _fs = null;
                return "ok";
            }
            catch (Exception ex)
            {
                string s = ex.Message;
                return s;
            }

        }
        //
    }
}
